import javax.microedition.lcdui.Graphics;

public class ChessBoard
{
public static int smallQuareWidth = 2;	
public static int s_chessBoardStartPosX;	
public static int s_chessBoardStartPosY;
public static int s_chessBoardWidth ;	
public static int s_chessBoardHeight;	

//the cursor 's psoition
public static int s_indexRow;
public static int s_indexCol;
public static int s_posX;
public static int s_posY;

public static Point m_cursor = new Point();

public ChessBoard()
{
	m_cursor.m_posX = -1;
	m_cursor.m_posY = -1;
}

static public void drawChessBoard(Graphics g,int xStartPos,int yStartPos, int witdh ,int height)
{
	int colorBg = 0xC68242;//some yellow
	int  colorLine = 0x422000;
	
	if(witdh > height) 
		witdh = height;
	else
		height = witdh;  // keep the chessborad is quare!!

	/// this draw bg 
////##	g.setColor(colorBg);
	int sideSpace = 5;
////##	g.fillRect(xStartPos - sideSpace, yStartPos - sideSpace, witdh + 2*sideSpace, height + 2*sideSpace);
	
	//recount the suitable xStart,yStart,width ,height for chress board
	int lineVSpace = witdh /(DEF.CHESS_BOARD_COL -1);   //
	int lineHSpace ;////= height /(DEF.CHESS_BOARD_ROW -1);  //line horizontal
	int newLength;
	newLength = lineVSpace * (DEF.CHESS_BOARD_COL -1);
	smallQuareWidth = lineHSpace = lineVSpace;
	
	//reset this value
	xStartPos += (witdh - newLength)/2 ;
	s_chessBoardStartPosY = s_chessBoardStartPosX = yStartPos = xStartPos;

	s_chessBoardHeight = s_chessBoardWidth = witdh = height =newLength ;
	
	//
	g.setColor(colorLine);
	g.drawRect(xStartPos , yStartPos , witdh , height ); g.drawRect(xStartPos + 1 , yStartPos + 1, witdh , height );

	// draw col
	int startX01 = xStartPos + lineVSpace ;
	int startY01 = 	yStartPos;
	int endX01 = xStartPos + lineVSpace ;
	int endY01 = yStartPos + height;
	
	int i;
	for(i=0; i < DEF.CHESS_BOARD_COL -2 ; i++)
	{
		
	   g.setColor(colorLine);
	   g.drawLine(startX01 + i*lineVSpace, startY01, endX01 + i*lineVSpace, endY01);
	  //// drawChess(g,startX01 + i*lineVSpace, startY01,0x000000);
	}
	
    // draw row
	 startX01 = xStartPos ;
	 startY01 = yStartPos + lineHSpace ;
	 endX01 = xStartPos + witdh ;
	 endY01 = yStartPos + lineHSpace;
	for( i=0; i< DEF.CHESS_BOARD_ROW -2 ;i++)	
	{
	 g.setColor(colorLine);
	  g.drawLine(startX01 , startY01 + i*lineHSpace, endX01 , endY01+ i*lineHSpace);
	 //// drawChess(g,endX01 , endY01+ i*lineHSpace,0xffffff);
	}
	 
	////drawCursor(g,endX01 , endY01);
}

/*
 * x: posX Pixels
 * y : posY Pixels
 */
static public void drawChess(Graphics g,int x,int y,int color) //color =0 black;0xffffff white
{
	int halfLength = smallQuareWidth>>1;
	g.setColor(color);
	g.fillArc(x - halfLength, y - halfLength, smallQuareWidth, smallQuareWidth, 0, 360);
}

/*
 * rowIndex: Row index
 * colIndex : Col indeY
 */
static public void drawChess( int rowIndex,int colIndex,int color) //color =0 black;0xffffff white
{
	Index2Pos(rowIndex,colIndex);
	drawChess(GameEngine.getGraphics(),s_posX,s_posY,color);
}

/*
 * rowIndex: Row index
 * colIndex : Col indeY
 */
static public void drawCurrentChess( int rowIndex,int colIndex,int color) //color =0 black;0xffffff white
{
	Index2Pos(rowIndex,colIndex);
	drawChess(GameEngine.getGraphics(),s_posX,s_posY,color);
	drawArrow(s_posX,s_posY);
}

/*
 * x: posX Pixels
 * y : posY Pixels
 */
static public void drawArrow(int x, int y,int color)
{
	int quaterLength = (smallQuareWidth>>2);
	Graphics g = GameEngine.getGraphics();
	g.setColor(color);
	g.drawLine(x - quaterLength, y, x + quaterLength, y);
	g.drawLine(x , y - quaterLength, x , y + quaterLength);
}
static public void drawArrow(int x, int y)
{
	drawArrow(x,y,0xff0000); 	
}

/*
 * Point  :Index
 */
static public void drawChess(Point chess,int color)
{
	Index2Pos(chess.m_posX,chess.m_posY);
	drawChess(GameEngine.getGraphics(),s_posX,s_posY,color);
}
static public void drawCursor(Graphics g,int x,int y)////,int color) //color =0 black;0xffffff white
{
	int halfLength = smallQuareWidth>>1;
	if(GameEngine.s_gameFrame%5==0)
	{
	 g.setColor(0xff0000);
	}
	else
	 g.setColor(0x0000ff);	
	
	g.drawRect(x - halfLength, y - halfLength, smallQuareWidth, smallQuareWidth);
	
}

/*
 * Point  :Index
 */
static public void drawCursor(Point cursor)
{
	Index2Pos(cursor.m_posX,cursor.m_posY);
	drawCursor(GameEngine.getGraphics(),s_posX,s_posY);
}

static public void drawCursor(int x,int y)
{
	
}


static public void InitCursor()
{
	m_cursor.m_posX = 0;
	m_cursor.m_posY = 0;
}

static public void Pos2Index(int posX,int posY )
{
	s_indexRow = (posX - s_chessBoardStartPosX)/smallQuareWidth ;
	s_indexCol = (posY - s_chessBoardStartPosY)/smallQuareWidth ;
}

static public void Index2Pos(int indexRow,int indexCol )
{
	s_posX = smallQuareWidth * indexRow + s_chessBoardStartPosX;
	s_posY = smallQuareWidth * indexCol + s_chessBoardStartPosY ;
}

static public int getIndexRow()
{
	return s_indexRow;
}
static public int getIndexCol()
{
	return s_indexCol;
}
static public int getPosX()
{
	return s_posX;
}
static public int getPosY()
{
	return s_posY;
}


static public void UpdateCursor()
{
	if(!GameInputKey.anykeyIsPressed())
		return ;
	
	int halfLength = smallQuareWidth>>1;
   
	if(GameInputKey.keyIsPressed(DEF.MOV1))  //control the Cursor
	{
		if(GameInputKey.keyIsPressed(DEF.NUM2| DEF.UP))
		{
			m_cursor.m_posY = (m_cursor.m_posY + DEF.CHESS_BOARD_ROW - 1) % DEF.CHESS_BOARD_ROW;
		}
		else if(GameInputKey.keyIsPressed(DEF.NUM8| DEF.DOWN))
		{
			m_cursor.m_posY = (m_cursor.m_posY+ DEF.CHESS_BOARD_ROW + 1) % DEF.CHESS_BOARD_ROW;
		}
		else if(GameInputKey.keyIsPressed(DEF.NUM4| DEF.LEFT))
		{
			m_cursor.m_posX = (m_cursor.m_posX + DEF.CHESS_BOARD_COL- 1) % DEF.CHESS_BOARD_COL;
		}
		else if(GameInputKey.keyIsPressed(DEF.NUM6| DEF.RIGHT))
		{
			m_cursor.m_posX = (m_cursor.m_posX + DEF.CHESS_BOARD_COL + 1) % DEF.CHESS_BOARD_COL;
		}
			
	}
	
}


}